package com.allwees.bs.module.pms.req.query;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.core.modelbase.req.QueryReq;
import com.allwees.bs.module.core.req.BranchUserQuery;
import com.allwees.bs.module.pms.entity.Product;
import com.allwees.core.common.constant.EStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Product查询条件
 *
 * @author yangylsky
 * @since 2019-12-18
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ProductQueryReq extends QueryReq<Product> implements BranchUserQuery<Product> {
	protected String supplierUuid;

	protected String retailerUuid;

	protected String storeUuid;

	private EProductStatus status;

	// 发布时间开始
	private Date publishAtFrom;

	// 发布时间结束
	private Date publishAtTo;

	/** 是否已更新 **/
	private Boolean updated;

	/** 定义编号 **/
	private String defNo;

	/**
	 * 商品uuid
	 */
	private String uuid;
	/**
	 * 售价区间
	 */
	private Double priceFrom;
	private Double priceTo;
	/**
	 * 类目编号
	 */
	private String categoryNo;

	private List<String> cates;

	/**
	 * 禁止加价
	 */
	private Integer forbiddenAdjustPrice;

	private Integer marketingType;

	@Override
	public void buildQuery(QueryWrapper<Product> queryWrapper) {
		queryWrapper
				.eq(StringUtils.isNotBlank(uuid),"uuid",uuid)
				.eq(StringUtils.isNotBlank(supplierUuid),"supplier_uuid",supplierUuid)
				.eq(StringUtils.isNotBlank(retailerUuid),"retailer_uuid",retailerUuid)
				.and(priceFrom != null,wrapper -> wrapper.ge("retail_price",priceFrom))
				.and(priceTo != null,wrapper -> wrapper.le("retail_price",priceTo))
				.in(CollectionUtil.isNotEmpty(cates),"category_no",cates)
				.eq(forbiddenAdjustPrice != null, "forbidden_adjust_price", forbiddenAdjustPrice)
				.eq(marketingType != null, "marketing_type & " + marketingType, marketingType)
		        .and(StringUtils.isNotBlank(q), wrapper -> {
				//wrapper.eq("uuid", q)
					wrapper.eq("no", q)
							.or().eq("custom_no", q)
							.or().eq("name", q);
			});

		buildBranchUserQuery(queryWrapper)
			.in(status != null,"status",status != null ? status.entityStatus() : null)
			.ge(publishAtFrom != null, "publish_at", publishAtFrom)
			.le(publishAtTo != null, "publish_at", publishAtTo)
			.eq(StringUtils.isNotBlank(defNo), "def_no", defNo)
			.and(updated != null, wrapper -> {
				if(updated != null && updated) {
					wrapper.gt("no_updated_count", 0);
				} else {
					wrapper.eq("no_updated_count", 0);
				}
			});
	}

	private enum EProductStatus {
		ON_CREATE {
			@Override
			List<Integer> entityStatus() {
				return Arrays.asList(
						EStatus.CREATED.value()
					);
			}
		},
		WAIT_AUDIT {
			@Override
			List<Integer> entityStatus() {
				return Arrays.asList(
						EStatus.WAIT_AUDIT.value()
					);
			}
		},
		AUDIT_REJECTED {
			@Override
			List<Integer> entityStatus() {
				return Arrays.asList(
						EStatus.AUDIT_REJECTED.value()
					);
			}
		},
		WAIT_PUTON {
			@Override
			List<Integer> entityStatus() {
				return Arrays.asList(
						EStatus.WAIT_PUTON.value(),
						EStatus.AUDIT_APPROVED.value()
					);
			}
		},
		ON_PUTOFF {
			@Override
			List<Integer> entityStatus() {
				return Arrays.asList(
						EStatus.PUTOFF_ONESELF.value(),
						EStatus.PUTOFF_QA_FAILURE.value(),
						EStatus.PUTOFF_OTHERS.value(),
						EStatus.PUTOFF_SELLOUT.value(),
						EStatus.PUTOFF_EDITING.value()
					);
			}
		},
		ON_SALE {
			@Override
			List<Integer> entityStatus() {
				return Arrays.asList(EStatus.NORMAL.value());
			}
		};

		abstract List<Integer> entityStatus();
	}
}
